package com.ruoyi.standingbook.sanitationvehicle.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.standingbook.sanitationvehicle.BillFlowUtil;
import com.ruoyi.standingbook.sanitationvehicle.domain.bo.SanitationVehicleBillBo;
import com.ruoyi.standingbook.sanitationvehicle.domain.bo.SanitationVehicleBo;
import com.ruoyi.standingbook.sanitationvehicle.domain.vo.BillReceiveBean;
import com.ruoyi.standingbook.sanitationvehicle.domain.vo.SanitationVehicleBillVo;
import com.ruoyi.standingbook.sanitationvehicle.domain.vo.SanitationVehicleVo;
import com.ruoyi.standingbook.sanitationvehicle.service.ISanitationVehicleBillService;
import com.ruoyi.standingbook.sanitationvehicle.service.ISanitationVehicleImportDetailService;
import com.ruoyi.standingbook.sanitationvehicle.service.ISanitationVehicleService;
import com.ruoyi.standingbook.v2toilet.ToiletConst;

import com.ruoyi.standingbook.v2toilet.enums.ItemStatusEnum;
import com.ruoyi.standingbook.v2toilet.logic.V2ExtendAsyncService;
import com.ruoyi.system.domain.SysOss;
import com.ruoyi.system.service.ISysConfigService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

@RequiredArgsConstructor
@Service
@Slf4j
public class SanitationVehicleExtendProcessService {
	private final ISanitationVehicleService sanitationVehicleService;
	private final ISanitationVehicleImportDetailService sanitationVehicleImportDetailService;
	private final ISanitationVehicleBillService sanitationVehicleBillService;
	private final ISysConfigService configService;    
	private final V2ExtendAsyncService apiLogService;

	private final static Map<String, String> map = new HashMap<String, String>();
	
	static {
		map.put("vehiclePaintworkStatus", "carPaintworkImage");
		map.put("licensePlateStatus", "licensePlateImage");
		map.put("bumperStatus", "carBumperImage");
		map.put("appearanceStatus", "vehicleAppearanceImage");
		map.put("airtightTransStatus", "airtightTransImage");
		
		map.put("cansClosedStatus", "cansClosedImage");
		map.put("dressBehaviorStatus", "dressBehaviorImage");
		map.put("hornWiperStatus", "hornWiperImage");
		map.put("gasPedalStatus", "gasPedalImage");
		map.put("lubricantStatus", "lubricantImage");
		
		map.put("coolantStatus", "coolantImage");
		map.put("waterVolumeStatus", "waterVolumeImage");
		map.put("reflectingBoardStatus", "reflectingBoardImage");
		map.put("tripodStatus", "tripodImage");
		map.put("safetySignsStatus", "safetySignsImage");
		
		map.put("reflectingMirrorStatus", "reflectingMirrorImage");
		map.put("extinguishersStatus", "extinguishersImage");
		map.put("tiresStatus", "tiresImage");
		map.put("brakeStatus", "brakeImage");
		map.put("rearviewMirrorStatus", "rearviewMirrorImage");
		
		map.put("steeringWheelsStatus", "steeringWheelsImage");
		map.put("brakePedalStatus", "brakePedalImage");
		map.put("hornStatus", "hornImage");
		map.put("lampStatus", "lampImage");
		map.put("hydraulicElevatorStatus", "hydraulicElevatorImage");
		
		map.put("alcoholTestStatus", "alcoholTestImage");
		map.put("licensePlateStatus1", "licensePlateImage1");
		map.put("driverLicenseStatus", "driverLicenseImage");
		map.put("tripCriteriaStatus", "tripCriteriaImage");
	}

	@Transactional
	public R<Void> receiveBill(final BillReceiveBean receiveData) {
		log.info(" api.receive<<<<<<<< {}", JSONUtil.toJsonStr(receiveData));
		if (Objects.isNull(receiveData.getFlowCode())) {
			log.warn("api.receive 流水号不能为空！");
			return R.fail("流水号不能为空");
		}
		// 根据
		SanitationVehicleBillBo bo = new SanitationVehicleBillBo();
		bo.setFlowCode(receiveData.getFlowCode());
		
		bo.setStatus(ItemStatusEnum.COMMIT.intCode);// 99
		bo.setModified(ToiletConst.MODIFY_NOT);// 1
		SanitationVehicleBillVo tmpVo = sanitationVehicleBillService.queryOne(bo);
		if (Objects.isNull(tmpVo)) {
			log.warn("api.receive 巡检项数据未找到！");
			return R.warn("巡检项数据未找到");
		}

		SysOss oss = null;
		String afterImage = null;
		if (StringUtils.isNotBlank(receiveData.getImage())) {
			oss = apiLogService.bytesUpload(receiveData.getFlowCode(), receiveData.getImage());
		}
		// 设置主表
		SanitationVehicleBo sanitationVehicleBo = new SanitationVehicleBo();
		// -------设置巡检项为绿色正常
		JSONObject jsonObject = JSONUtil.parseObj(sanitationVehicleBo);
		if (Objects.nonNull(oss)) {
			String afterAttr = itemNameToItemImageName(tmpVo.getItem(), false);
			jsonObject.set(afterAttr, oss.getOssId());
			System.out.println(jsonObject.get(afterAttr));
			afterImage = oss.getUrl();
		}
		jsonObject.set(tmpVo.getItem(), receiveData.getStatus());
		sanitationVehicleBo = JSONUtil.toBean(jsonObject, SanitationVehicleBo.class);
		// -------设置巡检项为绿色正常 END
		sanitationVehicleBo.setId(tmpVo.getSanitationVehicleId());
		sanitationVehicleBo.setModified(ToiletConst.MODIFY_NOT); // 设置本条数据已做过调整，不可覆盖
		SanitationVehicleVo dbData = sanitationVehicleService.queryById(tmpVo.getSanitationVehicleId());
		int processCount = (Objects.isNull(dbData.getAnomalyProcessCount()) ? 0 : dbData.getAnomalyProcessCount()) + 1;
		sanitationVehicleBo.setAnomalyProcessCount(
				processCount > dbData.getAnomalyCount() ? dbData.getAnomalyCount() : processCount);
		sanitationVehicleService.updateByBo(sanitationVehicleBo);
		// 设置主表 END
		// --------------------------------------------------------
		// 添加工单流程
		SanitationVehicleBillBo billNewFlow = BeanUtil.copyProperties(tmpVo, SanitationVehicleBillBo.class);
		billNewFlow.setCurrUser(receiveData.getUser());
		billNewFlow.setStatus(receiveData.getStatus());
		BillFlowUtil.setFlow(billNewFlow);// 设置流程
		billNewFlow.setImageBefore(null);
		if (StringUtils.isNotBlank(afterImage)) {
			billNewFlow.setImageAfter(afterImage);// 设置处理后图片
		} else {
			billNewFlow.setImageAfter(receiveData.getImage());
		}
		billNewFlow.setModified(ToiletConst.MODIFY_NOT);// 不可改变
		billNewFlow.setStatus(ItemStatusEnum.ERR_TO_OK.intCode);
		sanitationVehicleBillService.updateByBo(billNewFlow);
		// 添加工单流程 end
		log.info("billReceive process success");
		return R.ok();

	}

	
	public static String itemNameToItemImageName(String itemName, boolean beforeImage) {
		return map.get(itemName) + (beforeImage ? "Before" : "After");
	}
}
